package loads

import (
	"jvm/nzpJvm/instructions/base"
	"jvm/nzpJvm/runtime_area"
)

// 将局部变量表指定位置的引用类型变量压入操作数栈，操作数来自于字节码
type ALOAD struct{ base.Index8Instruction }

// 将局部变量表索引位置0处的引用类型变量压入操作数栈
type ALOAD_0 struct{ base.NoOperandsInstruction }
// 将局部变量表索引位置1处的引用类型变量压入操作数栈
type ALOAD_1 struct{ base.NoOperandsInstruction }
// 将局部变量表索引位置2处的引用类型变量压入操作数栈
type ALOAD_2 struct{ base.NoOperandsInstruction }
// 将局部变量表索引位置3处的引用类型变量压入操作数栈
type ALOAD_3 struct{ base.NoOperandsInstruction }

func (self *ALOAD) Execute(frame *runtime_area.Frame) {
	_aload(frame, self.Index)
}

func (self *ALOAD_0) Execute(frame *runtime_area.Frame) {
	_aload(frame, 0)
}

func (self *ALOAD_1) Execute(frame *runtime_area.Frame) {
	_aload(frame, 1)
}

func (self *ALOAD_2) Execute(frame *runtime_area.Frame) {
	_aload(frame, 2)
}

func (self *ALOAD_3) Execute(frame *runtime_area.Frame) {
	_aload(frame, 3)
}

func _aload(frame *runtime_area.Frame, index uint) {
	// 从栈帧的局部变量表中指定位置取出一个ref值，
	val := frame.LocalVars().GetRef(index)
	// 将其压到操作数栈中
	frame.OperandStack().PushRef(val)
}
